package com.quanyan.place.service.impl;

import com.alibaba.dubbo.common.utils.CollectionUtils;
import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;
import com.quanyan.api.APIResponse;
import com.quanyan.common.Plugin.Page;
import com.quanyan.common.Plugin.PageObj;
import com.quanyan.common.constants.Constants;
import com.quanyan.common.enums.SysParameterConfigEnum;
import com.quanyan.common.exception.BizException;
import com.quanyan.common.utils.CommonUtils;
import com.quanyan.common.utils.DateTimeUtils;
import com.quanyan.common.utils.DateUtils;
import com.quanyan.common.utils.ServletUtils;
import com.quanyan.orderpay.request.ReqOrderSave;
import com.quanyan.orderpay.request.ReqPayInfo;
import com.quanyan.orderpay.request.ReqProductDetail;
import com.quanyan.orderpay.response.RespPreOrder;
import com.quanyan.orderpay.service.OrderPayService;
import com.quanyan.place.constants.PlaceConstants;
import com.quanyan.place.entity.*;
import com.quanyan.place.entity.apireq.ReqMemberCardOperate;
import com.quanyan.place.entity.apireq.ReqMemberCardType;
import com.quanyan.place.entity.apireq.ReqOrderOperate;
import com.quanyan.place.entity.apireq.ReqPlaceMessageVo;
import com.quanyan.place.entity.apiresp.*;
import com.quanyan.place.entity.vo.PlaceMemberTypeVo;
import com.quanyan.place.enums.PlaceErrorCodeEnum;
import com.quanyan.place.mapper.*;
import com.quanyan.place.service.BaseService;
import com.quanyan.place.service.CardNoService;
import com.quanyan.place.service.PlaceMemberService;
import com.quanyan.place.service.PlaceMessageService;
import com.quanyan.user.response.RespUserInfoBase;
import com.quanyan.user.serviceFacade.UserServiceFacade;
import org.apache.commons.collections.MapUtils;
import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by youxiaojia on 2016/8/23.
 */
@Service("placeMessageService")
public class PlaceMessageServiceImpl implements PlaceMessageService {
    private Logger logger = LoggerFactory.getLogger(getClass());
    @Autowired
    TbBizPlaceNoticeMobileMapper  tbBizPlaceNoticeMobileMapper;

    @Autowired
    TbBizPlacePromotionRecordMapper  promotionRecordMapper;
    @Autowired
    TbBizPlacePromotionOrderMapper  promotionOrderMapper;
    @Autowired
    TbBizPlacePromotionRewardConfigMapper tbBizPlacePromotionRewardConfigMapper;
    @Autowired
    TbBizPlacePromotionTimesMapper  tbBizPlacePromotionTimesMapper;

    /**
     * 插入推送场地预定短信相关的数据
     * @param reqPlaceMessageVo
     */
    @Override
    public int insertMobileData(ReqPlaceMessageVo reqPlaceMessageVo) {
        TbBizPlaceNoticeMobile message=new TbBizPlaceNoticeMobile();
        message.setName(reqPlaceMessageVo.getName());
        message.setPlaceId(reqPlaceMessageVo.getPlaceId());
        message.setMobile(reqPlaceMessageVo.getMobile());
        message.setId(reqPlaceMessageVo.getId());
        if (reqPlaceMessageVo.getId()!=null){
           return tbBizPlaceNoticeMobileMapper.updateByPrimaryKey(message);
        }else{
           return  tbBizPlaceNoticeMobileMapper.insertSelective(message);
        }

    }

    /**
     * 查询短信相关的数据
     * @param  placeId
     * @return
     */
    @Override
    public TbBizPlaceNoticeMobile getMobileDataByPlaceId(Integer placeId) {
        TbBizPlaceNoticeMobileExample example =new TbBizPlaceNoticeMobileExample();
        TbBizPlaceNoticeMobileExample.Criteria criteria=example.createCriteria();
        criteria.andPlaceIdEqualTo(placeId);
        List<TbBizPlaceNoticeMobile> list=tbBizPlaceNoticeMobileMapper.selectByExample(example);
        if (list!=null&&list.size()>0){
            return list.get(0);
        }else{
            return null;
        }
    }

    /**
     * 删除数据
     * @param reqPlaceMessageVo
     * @return
     */
    @Override
    public int deleteMobileData(ReqPlaceMessageVo reqPlaceMessageVo) {
      return tbBizPlaceNoticeMobileMapper.deleteByPrimaryKey(reqPlaceMessageVo.getId());
    }

    @Override
    public int savePromotionRecord(TbBizPlacePromotionRecord vo) {
        return   promotionRecordMapper.insertSelective(vo);
    }

    @Override
    public int savePromotionOrder(TbBizPlacePromotionOrder vo) {
        return promotionOrderMapper.insertSelective(vo);

    }

    @Override
    public TbBizPlacePromotionRecord getPromotionRecordByToken(String token) {
        TbBizPlacePromotionRecordExample example =new TbBizPlacePromotionRecordExample();
        TbBizPlacePromotionRecordExample.Criteria criteria=example.createCriteria();
        criteria.andTokenEqualTo(token);
        List<TbBizPlacePromotionRecord> list=promotionRecordMapper.selectByExample(example);
        if (list!=null&&list.size()>0){
            return list.get(0);
        }else{
            return null;
        }
    }

    @Override
    public List<TbBizPlacePromotionOrder> getPromotionOrderByMobile(String mobile) {
        TbBizPlacePromotionOrderExample example =new TbBizPlacePromotionOrderExample();
        TbBizPlacePromotionOrderExample.Criteria criteria=example.createCriteria();
        criteria.andMobileEqualTo(mobile);
        criteria.andOrderStatusEqualTo((int)PlaceConstants.ORDER_COMPLETE_PAY_STATUS);
        List<TbBizPlacePromotionOrder> list=promotionOrderMapper.selectByExample(example);
            return list;

    }



    @Override
    public TbBizPlacePromotionRewardConfig getPromotionRewardInfo(TbBizPlacePromotionRewardConfig vo) {
        TbBizPlacePromotionRewardConfigExample example =new TbBizPlacePromotionRewardConfigExample();
        TbBizPlacePromotionRewardConfigExample.Criteria criteria=example.createCriteria();
        criteria.andPlaceIdEqualTo(vo.getPlaceId());
        criteria.andCategoryIdEqualTo(vo.getCategoryId());
        criteria.andTimesEqualTo(vo.getTimes());
        List<TbBizPlacePromotionRewardConfig> list=tbBizPlacePromotionRewardConfigMapper.selectByExample(example);
        if (list!=null&&list.size()>0){
            return list.get(0);
        }else{
            return null;
        }
    }

    @Override
    public List<TbBizPlacePromotionTimes> getPromotionTimesByMobile(String mobile) {
        TbBizPlacePromotionTimesExample example =new TbBizPlacePromotionTimesExample();
        TbBizPlacePromotionTimesExample.Criteria criteria=example.createCriteria();
        criteria.andMobileEqualTo(mobile);
        List<TbBizPlacePromotionTimes> list=tbBizPlacePromotionTimesMapper.selectByExample(example);
        return list;

    }

    @Override
    public int savePromotionTimes(TbBizPlacePromotionTimes vo) {
       return tbBizPlacePromotionTimesMapper.insertSelective(vo);
    }




}
